<!-- 用于标注任务表单选择标注类型 -->
<template>
  <div class="select-dmp-type">
    <div class="header">
      <a-radio-group :value="taskType" :options="taskTypeOptions" @update:value="updateTaskType" @change="onTaskTypeChange" />
    </div>
    <div class="des">请选择标注任务</div>
    <div class="sub-type-list">
      <div
        v-for="item in subTypeList"
        :key="item.label"
        class="sub-type-item"
        :class="{ active: item.value === subTaskType }"
        @click="updateSubTaskType(item.value)"
      >
        <img class="select-icon" :src="selectIconImg" />
        <div class="item-label">{{ item.label }}</div>
        <div class="item-des">{{ item.des }}</div>
        <img class="item-img" :src="item.imgUrl" />
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { computed, nextTick } from 'vue'

  import selectIconImg from '@/assets/img/dmp/type-select.svg'

  import { taskTypeOptions } from '../../options'
  import { DmpSubTaskType, DmpTaskType } from '../../types'

  export interface Props {
    // 标注任务类型
    taskType: DmpTaskType
    // 标注任务子类型
    subTaskType: DmpSubTaskType
  }
  const props = withDefaults(defineProps<Props>(), {})
  const emits = defineEmits<{
    (e: 'update:taskType', params: DmpTaskType): void
    (e: 'update:subTaskType', params: DmpSubTaskType): void
  }>()

  const subTypeList = computed(() => taskTypeOptions.find(it => it.value === props.taskType)?.children || [])

  const updateTaskType = (value: DmpTaskType) => emits('update:taskType', value)
  const updateSubTaskType = (value: DmpSubTaskType) => emits('update:subTaskType', value)
  const onTaskTypeChange = async () => {
    await nextTick()
    updateSubTaskType(subTypeList.value[0].value)
  }
</script>

<style lang="less" scoped>
  .select-dmp-type {
    background-color: #fff;
    border: 1px solid @border2;
    border-radius: 6px;

    .header {
      display: flex;
      align-items: center;
      padding: 0 24px;
      height: 44px;
      background: #fafafa;
      gap: 24px;
      align-self: stretch;
    }

    .des {
      display: flex;
      align-items: center;
      padding: 4px 24px;
      font-size: 12px;
      color: @text03;
      gap: 10px;
      font-weight: 400;
      line-height: 20px;
      border-bottom: 0.5px solid @border1;
    }

    .sub-type-list {
      display: grid;
      padding: 0 24px 8px;
      margin-top: 8px;
      grid-template-columns: repeat(auto-fill, 214px);
      gap: 6px;

      .sub-type-item {
        position: relative;
        overflow: hidden;
        padding: 6px 12px;
        background: #fff;
        border-radius: 4px;
        box-shadow: none;

        .select-icon {
          position: absolute;
          top: 0;
          right: -1px;
          display: none;
        }

        .item-label {
          color: @text01;
          .font14();
        }

        .item-des {
          color: @text03;
          letter-spacing: -0.1px;
          .font12();
        }

        .item-img {
          margin-top: 8px;
          width: 100%;
          height: 90px;
        }

        &:hover {
          box-shadow: 0 1px 4px 0 rgba(2, 36, 59, 0.06);
        }

        &.active {
          padding: 5px 11px;
          border: 1px solid #1868e9;

          .select-icon {
            display: block;
          }
        }
      }
    }
  }
</style>
